﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WCSY.BLL;
using WCSY.MODEL;
using System.Data.SqlClient;
using System.Data;

public partial class UserManagement_RoleList : System.Web.UI.Page
{
    Mg_roleManager roleM = new Mg_roleManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack){

            initTree();
            DataTable roleInfo = roleM.GetAllList().Tables[0];
            myBind(roleInfo);

            this.txtCreateDate.Text = DateTime.Now.Date.ToShortDateString();
            //保存按钮不可用
            this.btnSave.Enabled = false;
                
        }
    }

    //绑定父目录
    public void initTree()
    {

        DataTable parentDt = roleM.getParentRoleList();
        roleTreeView.Nodes.Clear();
        TreeNode desc = new TreeNode();
        desc.Value = "0";
        desc.Text = "岗位列表";
        roleTreeView.Nodes.Add(desc);
        foreach (DataRow row in parentDt.Rows)
        {
            TreeNode root = new TreeNode();
            string rootValue = row["roleName"].ToString();
            string rootId = row["id"].ToString();
            root.Text = rootValue;
            root.Value = rootId;


            desc.ChildNodes.Add(root);
            //root.ImageUrl = "~/image/base1.gif";
            //if (!t.Rightsurl.Equals(""))
            //{
            //    root.NavigateUrl = "JAVAscript:topage('../../" + t.Rightsurl + "');";
            //}

            childNodes(root);
        }

    }

    //绑定子目录
    public void childNodes(TreeNode childNode)
    {

        DataTable childDt = roleM.getChiledRoleList(childNode.Value);

        //childNode.SelectAction = TreeNodeSelectAction.Expand;

        foreach (DataRow row in childDt.Rows)
        {
            TreeNode child = new TreeNode();
            child.Text = row["roleName"].ToString();
            child.Value = row["id"].ToString();

            childNode.ChildNodes.Add(child);
            //child.ImageUrl = "~/image/base1.gif";
            //if (!c.Rightsurl.Equals(""))
            //{
            //    //child.NavigateUrl = "JAVAscript:openUrl('" + c.Rightsurl + "');";//href=\"javascript:topage('../../" + c.Rightsurl + "')\" 
            //    child.NavigateUrl = "JAVAscript:topage('../../" + c.Rightsurl + "');";
            //}
            //child.NavigateUrl = c.Rightsurl;

            childNodes(child);
        }


    }
    /// <summary>
    /// 绑定数据列表
    /// </summary>
    /// <param name="deptInfo"></param>
    public void myBind(DataTable roleInfo)
    {

        this.roleGridView.DataSource = roleInfo;

        this.roleGridView.DataBind();


    }

    //删除数据
    protected void btnDel_Click(object sender, EventArgs e)
    {

        if (this.pageFlag.Value.Equals(""))
        {
            tool.Alert("没有选中数据行，请选择！", this);
            return;
        }
        List<string> sqlList = new List<string>();
        int id = Convert.ToInt32(this.pageFlag.Value);
        sqlList.Add(roleM.Delete(id));
        try
        {
            string suc = tool.executeSqlTran(sqlList);
            if (!suc.Equals("suc"))
            {
                throw new Exception(suc);
            }
        }
        catch (Exception ex)
        {

            tool.Alert("信息删除失败!" + ex.Message, this);
            return;
        }
        DataTable dt = roleM.GetAllList().Tables[0];
        myBind(dt);
        tool.Alert("信息删除成功！", this);


    }
    //查询数据
    protected void secBtn_Click(object sender, EventArgs e)
    {

        //string deptNo = this.deptNoText.Text;
        //string deptName = this.deptNameText.Text;
        string key = this.keyword.Text.Trim();
        string where = "";
        if (!key.Equals(""))
        {
            where += " (roleid = '" + key + "' or rolename like '%" + key + "%')";
        }

        DataTable deptinfo = roleM.GetList(where).Tables[0];
        myBind(deptinfo);

    }
    //新增数据
    protected void btnAdd_Click(object sender, EventArgs e)
    {

        string maxno = tool.GetBaseMaxSysno("mg_role");
        this.txtroleId.Text = maxno;
        //绑定部门信息
        this.ddlsuPost.Items.Clear();
        this.ddlsuPost.DataSource = roleM.GetAllList().Tables[0];
        this.ddlsuPost.DataValueField = "id";
        this.ddlsuPost.DataTextField = "roleName";
        this.ddlsuPost.DataBind();
        ListItem item = new ListItem();
        item.Value = "0";
        item.Text = "-请选择-";
        ddlsuPost.Items.Insert(0, item);

        txtroleName.Text = "";
        txtCreateDate.Text = "";
        chkislogin.Checked = false;
        txtremarks.Text = "";
        
        this.pageFlag.Value = "";//设置页面状态为新增
        tool.SetControlReadOnlyNone(this);

        //设置按钮状态
        this.btnSave.Enabled = true;//保存
        this.btnUpd.Enabled = false;//修改
        this.btnDel.Enabled = false;//删除
        this.btnAdd.Enabled = false;//新建


    }
    //保存数据
    protected void btnSave_Click(object sender, EventArgs e)
    {
        int maxid = tool.GetBaseMaxId("id", "mg_role");
        string maxno = tool.GetBaseMaxSysno("mg_role");
        List<string> sqlList = new List<string>();
        try
        {
            mg_role role = new mg_role();


            role.roleName = txtroleName.Text;
            role.createDate = Convert.ToDateTime(txtCreateDate.Text);
            role.lgSign = chkislogin.Checked?0:1;
            
            role.suPost = Convert.ToInt32(ddlsuPost.SelectedValue);
            role.remarks = txtremarks.Text;
            if (this.pageFlag.Value.Equals(""))
            {//新增
                role.id = maxid;
                role.roleId = maxno;
                sqlList.Add(roleM.Add(role));
            }
            else
            {//修改
                role.id = Convert.ToInt32(this.pageFlag.Value);
                role.roleId = txtroleId.Text;
                sqlList.Add(roleM.Update(role));
            }

        }
        catch (Exception ex)
        {
            tool.Alert("信息保存失败！" + ex.Message, this);
            return;

        }
        try
        {
            string suc = tool.executeSqlTran(sqlList);
            if (!suc.Equals("suc"))
            {
                throw new Exception(suc);
            }
        }
        catch (Exception ex)
        {

            tool.Alert("信息保存失败!" + ex.Message, this);
            return;
        }
        //设置按钮状态
        this.btnSave.Enabled = false;//保存
        this.btnUpd.Enabled = true;//修改
        this.btnDel.Enabled = true;//删除
        this.btnAdd.Enabled = true;//新建
        tool.SetControlReadOnly(this);
        tool.SetControlReadOnlyNone(keyword);
        tool.SetControlReadOnlyNone(forkeyword);
        DataTable dt = roleM.GetAllList().Tables[0];
        myBind(dt);
        tool.Alert("信息保存成功！", this);

    }
    //绑定行选定事件
    protected void roleGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[7].Visible = false;
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "if(this!=prevselitem){this.style.backgroundColor='#Efefef'}");//当鼠标停留时更改背景色
            e.Row.Attributes.Add("onmouseout", "if(this!=prevselitem){this.style.backgroundColor='#ffffff'}");//当鼠标移开时还原背景色
            e.Row.Attributes.Add("onclick", "this.cells[0].childNodes[0].click();");//selectx(this);

        }

    }
    //选中行绑定明细数据
    protected void roleGridView_SelectedIndexChanged(object sender, EventArgs e)
    {

        int id = Convert.ToInt32(roleGridView.SelectedDataKey[0]);
        this.pageFlag.Value = id.ToString();//隐藏控件pageFlag ,为判断当前为修改或是新增，如果为修改，则隐藏控件值不为空，为选中行id ,反之为新增状态。
        //绑定部门信息
        this.ddlsuPost.Items.Clear();
        this.ddlsuPost.DataSource = roleM.GetAllList().Tables[0];
        this.ddlsuPost.DataValueField = "id";
        this.ddlsuPost.DataTextField = "roleName";
        this.ddlsuPost.DataBind();
        ListItem item = new ListItem();
        item.Value = "0";
        item.Text = "-请选择-";
        ddlsuPost.Items.Insert(0, item);

        mg_role role = roleM.GetModel(id);
        txtroleId.Text = role.roleId;
        txtroleName.Text = role.roleName;
        txtCreateDate.Text = Convert.ToDateTime(role.createDate).ToShortDateString();
        chkislogin.Checked = role.lgSign == 1 ? false : true;
        ddlsuPost.SelectedValue = role.suPost.ToString();
        txtremarks.Text = role.remarks;
        
        

        tool.SetControlReadOnly(this);
        tool.SetControlReadOnlyNone(keyword);
        tool.SetControlReadOnlyNone(forkeyword);
        //设置按钮状态
        this.btnSave.Enabled = false;//保存
        this.btnUpd.Enabled = true;//修改
        this.btnDel.Enabled = true;//删除
        this.btnAdd.Enabled = true;//新建


    }
    //修改按钮，修改控件编辑状态
    protected void btnUpd_Click(object sender, EventArgs e)
    {
        //设置所有控件取消只读状态
        tool.SetControlReadOnlyNone(this);
        //设置保存按钮可访问，并隐藏修改按钮
        this.btnSave.Enabled = true;//保存
        this.btnUpd.Enabled = false;//修改
        this.btnDel.Enabled = false;//删除
    }
    //选择节点后，绑定gridview数据
    protected void roleTreeView_SelectedNodeChanged(object sender, EventArgs e)
    {
        string idvalue = roleTreeView.SelectedNode.Value;
        if (idvalue.Equals("0"))
        {
            DataTable dt = roleM.GetAllList().Tables[0];
            myBind(dt);
        }
        else
        {

            int id = Convert.ToInt32(idvalue);
            DataTable dt = roleM.GetList(" id = " + id + "").Tables[0];
            myBind(dt);
            //默认选中行
            roleGridView.SelectedIndex = 0;
            //CommandEventArgs cm=new CommandEventArgs("choose",(deptListGridView.Rows[0].FindControl("LinkButton1") as LinkButton).CommandArgument);
            //GridViewCommandEventArgs arg=new GridViewCommandEventArgs(deptListGridView.Rows[0],cm);
            //deptListGridView_RowCommand(null, arg);

           roleGridView_SelectedIndexChanged(sender, e);
        }
    }
}
